/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.google.common.collect.HashMultimap;
/*     */ import com.google.common.collect.Multimap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.db.DbBlobConsistency;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.ExportAndDeleteItemsRequest;
/*     */ import com.zimbra.soap.admin.type.ExportAndDeleteItemSpec;
/*     */ import com.zimbra.soap.admin.type.ExportAndDeleteMailboxSpec;
/*     */ import java.io.File;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExportAndDeleteItems
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element requst, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  49 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  50 */     checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
/*     */     
/*     */ 
/*  53 */     ExportAndDeleteItemsRequest req = (ExportAndDeleteItemsRequest)JaxbUtil.elementToJaxb(requst);
/*  54 */     ExportAndDeleteMailboxSpec mailbox = req.getMailbox();
/*  55 */     if (mailbox == null) {
/*  56 */       throw ServiceException.INVALID_REQUEST("empty mbox id", null);
/*     */     }
/*  58 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mailbox.getId());
/*  59 */     Multimap<Integer, Integer> idRevs = HashMultimap.create();
/*  60 */     for (ExportAndDeleteItemSpec item : mailbox.getItems()) {
/*  61 */       idRevs.put(Integer.valueOf(item.getId()), Integer.valueOf(item.getVersion()));
/*     */     }
/*  63 */     String dirPath = req.getExportDir();
/*  64 */     String prefix = req.getExportFilenamePrefix();
/*     */     
/*     */ 
/*  67 */     mbox.lock.lock();
/*     */     try {
/*  69 */       DbPool.DbConnection conn = null;
/*     */       try
/*     */       {
/*  72 */         conn = DbPool.getConnection();
/*  73 */         if (dirPath != null) {
/*  74 */           File exportDir = new File(dirPath);
/*  75 */           if (!exportDir.isDirectory()) {
/*  76 */             DbPool.quietClose(conn);
/*  77 */             throw ServiceException.INVALID_REQUEST(dirPath + " is not a directory", null);
/*     */           }
/*     */           
/*  80 */           String filePath = makePath(dirPath, "mail_item", prefix);
/*  81 */           export(conn, mbox, "mail_item", "id", idRevs, filePath);
/*  82 */           filePath = makePath(dirPath, "mail_item_dumpster", prefix);
/*  83 */           export(conn, mbox, "mail_item_dumpster", "id", idRevs, filePath);
/*     */           
/*  85 */           filePath = makePath(dirPath, "revision", prefix);
/*  86 */           export(conn, mbox, "revision", "item_id", idRevs, filePath);
/*  87 */           filePath = makePath(dirPath, "revision_dumpster", prefix);
/*  88 */           export(conn, mbox, "revision_dumpster", "item_id", idRevs, filePath);
/*     */           
/*  90 */           filePath = makePath(dirPath, "appointment", prefix);
/*  91 */           export(conn, mbox, "appointment", "item_id", idRevs, filePath);
/*  92 */           filePath = makePath(dirPath, "appointment_dumpster", prefix);
/*  93 */           export(conn, mbox, "appointment_dumpster", "item_id", idRevs, filePath);
/*     */         }
/*     */         
/*     */ 
/*  97 */         for (Iterator i$ = idRevs.keySet().iterator(); i$.hasNext();) { itemId = (Integer)i$.next();
/*  98 */           revs = idRevs.get(itemId);
/*  99 */           for (i$ = revs.iterator(); i$.hasNext();) { int rev = ((Integer)i$.next()).intValue();
/* 100 */             if (rev == 0)
/*     */             {
/* 102 */               List<MailItem> list = null;
/*     */               try {
/* 104 */                 list = mbox.getAllRevisions(null, itemId.intValue(), MailItem.Type.UNKNOWN);
/*     */               }
/*     */               catch (MailServiceException.NoSuchItemException ex) {}
/* 107 */               continue;
/*     */               
/*     */ 
/* 110 */               for (MailItem item : list) {
/* 111 */                 if (item.getType() == MailItem.Type.DOCUMENT) {
/* 112 */                   mbox.purgeRevision(null, itemId.intValue(), item.getVersion(), false);
/*     */                 }
/*     */               }
/* 115 */               mbox.delete(null, itemId.intValue(), MailItem.Type.UNKNOWN, null);
/* 116 */               break;
/* 117 */             } else if (!revs.contains(Integer.valueOf(0))) {
/*     */               try {
/* 119 */                 mbox.purgeRevision(null, itemId.intValue(), rev, false);
/*     */               }
/*     */               catch (MailServiceException.NoSuchItemException ex) {}
/*     */             }
/*     */           }
/*     */         }
/*     */         
/*     */         Integer itemId;
/*     */         Collection<Integer> revs;
/*     */         Iterator i$;
/* 129 */         DbBlobConsistency.delete(conn, mbox, idRevs);
/*     */       } finally {
/* 131 */         conn.commit();
/* 132 */         DbPool.quietClose(conn);
/*     */       }
/*     */     } finally {
/* 135 */       mbox.lock.release();
/*     */     }
/*     */     
/* 138 */     return zsc.createElement(AdminConstants.EXPORT_AND_DELETE_ITEMS_RESPONSE);
/*     */   }
/*     */   
/*     */   private void export(DbPool.DbConnection conn, Mailbox mbox, String tableName, String idColName, Multimap<Integer, Integer> idRevs, String filePath)
/*     */     throws ServiceException
/*     */   {
/* 144 */     if (DbBlobConsistency.getNumRows(conn, mbox, tableName, idColName, idRevs) > 0) {
/* 145 */       DbBlobConsistency.export(conn, mbox, tableName, idColName, idRevs, filePath);
/*     */     }
/*     */   }
/*     */   
/*     */   String makePath(String dirPath, String tableName, String prefix) {
/* 150 */     if (prefix == null) {
/* 151 */       prefix = "";
/*     */     }
/* 153 */     return dirPath + "/" + prefix + tableName + ".txt";
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 158 */     notes.add("Only system admins are allowed.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/ExportAndDeleteItems.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */